package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.StrategyCommend;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IStrategyCommendService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.service.IStrategyTagService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.key.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IStrategyRedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyRedisService strategyRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateSearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends",top5);
        //所有攻略的vo排行
        List<StrategyStatisVO> listVo =  strategyRedisService.getAllVo(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(),0,-1);
        //abroadCds 海外推荐
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        //unabroadCds 国内推荐
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : listVo) {
            //判断是否是海外
            if(vo.isIsabroad()){
                //判断是否超过10
                if(abroadCds.size() < 10){
                    abroadCds.add(vo);
                }
            }else{
                if(unabroadCds.size() < 10){
                    unabroadCds.add(vo);
                }
            }
        }
        //共享
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);

        //热门攻略排行 hotCds
        List<StrategyStatisVO> hotVo = strategyRedisService.getAllVo(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(), 0, 9);
        model.addAttribute("hotCds",hotVo);



        //chinas 国内攻略统计数据
        List<StatisVO> chinas =  strategySearchService.queryCondition(StrategySearchQuery.CONDITION_CHINA);
        //abroads 海外攻略统计数据
        List<StatisVO> abroads = strategySearchService.queryCondition(StrategySearchQuery.CONDITION_ABROAD);
        //themes 主题统计数据
        List<StatisVO> themes = strategySearchService.queryCondition(StrategySearchQuery.CONDITION_THEME);
        model.addAttribute("chinas",chinas);
        model.addAttribute("abroads",abroads);
        model.addAttribute("themes",themes);


        //主题推荐
        //List(0)   ------>  map ---->   {map.put("theme",statisVo) --->户外  map.put("dests",list) -->广州  上海}
        List<Map<String,Object>> themeCds = strategySearchService.queryThemeCommend();
        model.addAttribute("themeCds",themeCds);

        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo")StrategySearchQuery qo){

        Page page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }



    @RequestMapping("/detail")
    public String detail(Model model, Long id,@UserParam UserInfo user) {
        //detail
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        strategyDetail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail", strategyDetail);
        //阅读数+1
        strategyRedisService.viewNumIncrease(id, 1);
        //热门攻略排行分数+1
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),
                RedisKeys.STRATEGY_STATIS_VO.join(id.toString()),1);
        //vo
        StrategyStatisVO vo = strategyRedisService.getStatisvo(id);
        model.addAttribute("vo", vo);
        //isFavor
        if(user != null){
            boolean flag = strategyRedisService.ifFavor(id,user.getId());
            if(flag){
               model.addAttribute("isFavor",flag);
            }
        }
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        //tags
        model.addAttribute("tags", strategyTagService.list());

        //pageInfo
        //qo.setDestId(-1L);
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo user) {
        comment.setUserId(user.getId());
        comment.setLevel(user.getLevel());
        comment.setCity(user.getCity());
        comment.setHeadUrl(user.getHeadImgUrl());
        comment.setUsername(user.getNickname());
        //添加进去
        strategyCommentService.saveOrUpdate(comment);

        //将被评论的攻略评论数增加
//        Long detailId = comment.getDetailId();
//        strategyDetailService.updateReplyNumByDetailId(detailId,1);
        //评论数 + 1
        strategyRedisService.replyNumIncrease(comment.getDetailId(), 1);
        //热门攻略排行分数+1
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),
                RedisKeys.STRATEGY_STATIS_VO.join(comment.getDetailId().toString()),1);
        StrategyStatisVO vo = strategyRedisService.getStatisvo(comment.getDetailId());
        return AjaxResult.SUCCESS().addData(vo.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {

        Page page = strategyCommentService.query(qo);

        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    /**
     * @param toid   攻略id
     * @param fromid 用户的id
     * @return
     */
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {

        strategyCommentService.commentThumbUp(toid, fromid);
        return AjaxResult.SUCCESS();
    }

    //收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先进行登录");
            result.setCode(102);
            return result;
        }


        boolean ret = strategyRedisService.favor(sid, userInfo.getId());
        //攻略排行分数+1或-1
        strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(),
                RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),ret?1:-1);
        return new AjaxResult(ret, "").addData(strategyRedisService.getStatisvo(sid));
    }

    //攻略的顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先进行登录");
            result.setCode(102);
            return result;
        }
        //操作是否有效
        boolean ret = strategyRedisService.thumbup(sid, userInfo.getId());
        //有效
        if(ret){
            //攻略排行分数+1
            strategyRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefix(),
                    RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),1);
        }
        return new AjaxResult(ret, "").addData(strategyRedisService.getStatisvo(sid));
    }

    @RequestMapping("/share")
    @ResponseBody
    public Object share(Long sid) {
        strategyRedisService.shareNumIncrease(sid,1);
        return AjaxResult.SUCCESS().addData(strategyRedisService.getStatisvo(sid).getSharenum());
    }
}
